package com.aring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.aring.bean.User;
import com.aring.dao.UserDao;

@Controller
public class UserController{
	
	@RequestMapping(value="/register")
	public String registerView(){
		return "register";
	}
	
	@RequestMapping(value="/register.do",method=RequestMethod.POST)
	public String register(User user,Model model){
		int result = 0;
 	    UserDao userDao = new UserDao();
		result = userDao.saveUser(user);
		if(result==0){
			model.addAttribute("message", "Register fail !");
			return "error";
		}else {
			model.addAttribute("message", "Register successful !");	
			model.addAttribute("user", user);
			return "success";
		}
	}
	
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String loginView(){
		return "login";
	}
	
	@RequestMapping(value="/login.do",method=RequestMethod.POST)
	public String login(User user,Model model){      
	    UserDao userDao = new UserDao();
	    user = userDao.loginUser(user);
		if(user!=null){
			model.addAttribute("message", "Login Success");
			model.addAttribute("user", user);
			return "success";

		}else {
			model.addAttribute("message", "Username or password is wrong!");
			return "error";
		  }
	}

}
